package com.xms.ribbonbutton.service;

import com.xms.core.components.ValueRule;
import com.xms.core.data.Entity;
import com.xms.form.core.FormState;
import com.xms.ribbonbutton.core.CommandDefinition;
import com.xms.ribbonbutton.entity.RibbonButton;
import com.xms.sdk.core.ValueConverter;
import com.xms.utils.CollectionUtil;
import com.xms.utils.JsonUtil;
import com.xms.utils.StringUtil;

/**
 * <p>
 * RibbonButtonExtensions
 * </p>
 *
 * @author migo
 * @since 2021/9/9
 */
public class RibbonButtonExtensions {
    // 设置按钮可用/可见状态
    public static void setButtonStatus(RibbonButton button, FormState formState, Entity record)
    {
        boolean enabled = true;
        boolean visibled = true;
        if (StringUtil.notEmpty(button.getCommandrules()))
        {
            CommandDefinition rules = JsonUtil.toBean(button.getCommandrules(), CommandDefinition.class);
            if (formState != null && rules.FormStateRules != null
                    && CollectionUtil.notEmpty(rules.FormStateRules.States))
            {
                enabled = !rules.FormStateRules.Enabled;
                visibled = !rules.FormStateRules.Visibled;
                for (FormState item : rules.FormStateRules.States)
                {
                    if (item == formState)
                    {
                        enabled = rules.FormStateRules.Enabled;
                        visibled = rules.FormStateRules.Visibled;
                        if (!visibled)
                        {
                            break;
                        }
                    }
                }
            }
            if (visibled && record != null && rules.ValueRules != null
                    && CollectionUtil.notEmpty(rules.ValueRules.Values))
            {
                enabled = !rules.ValueRules.Enabled;
                visibled = !rules.ValueRules.Visibled;
                for (ValueRule item : rules.ValueRules.Values)
                {
                    //if null value
                    if (item.Value.equalsIgnoreCase("null") && record.getValue(item.Field) == null)
                    {
                        enabled = rules.ValueRules.Enabled;
                        visibled = rules.ValueRules.Visibled;
                        if (!visibled)
                        {
                            break;
                        }
                    }
                    else if(record.getValue(item.Field) instanceof Boolean){
                        Boolean tmpBl = ValueConverter.toBoolean(item.Value);
                        if(tmpBl.equals(record.getValue(item.Field))){
                            enabled = rules.ValueRules.Enabled;
                            visibled = rules.ValueRules.Visibled;
                            if (!visibled)
                            {
                                break;
                            }
                        }
                    }
                    else if(item.Value.equalsIgnoreCase(record.getStringValue(item.Field))){
                        enabled = rules.ValueRules.Enabled;
                        visibled = rules.ValueRules.Visibled;
                        if (!visibled)
                        {
                            break;
                        }
                    }
                }
            }
        }
        button.isenabled = enabled;
        button.isvisibled = visibled;
    }
}
